home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 July / CHIP 2006-07.2.iso / program / web_gelistirme / easyphp1-7_setup.exe / {app} / phpmyadmin / pdf_schema.php < prev    next >
Encoding:
PHP Script  |  2003-09-07  |  51.7 KB  |  1,560 lines

  1. <?php
  2. /* $Id: pdf_schema.php,v 1.56 2003/07/10 13:10:53 lem9 Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5.  
  6. /**
  7.  * Contributed by Maxime Delorme and merged by lem9
  8.  */
  9.  
  10.  
  11. /**
  12.  * Gets some core scripts
  13.  */
  14. require('./libraries/grab_globals.lib.php');
  15. require('./libraries/common.lib.php');
  16.  
  17.  
  18. /**
  19.  * Settings for relation stuff
  20.  */
  21. require('./libraries/relation.lib.php');
  22. require('./libraries/transformations.lib.php');
  23.  
  24. $cfgRelation = PMA_getRelationsParam();
  25.  
  26.  
  27. /**
  28.  * Now in ./libraries/relation.lib.php we check for all tables
  29.  * that we need, but if we don't find them we are quiet about it
  30.  * so people can work without.
  31.  * This page is absolutely useless if you didn't set up your tables
  32.  * correctly, so it is a good place to see which tables we can and
  33.  * complain ;-)
  34.  */
  35. if (!$cfgRelation['pdfwork']) {
  36.     echo '<font color="red">' . $strError . '</font><br />' . "\n";
  37.     $url_to_goto = '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">';
  38.     echo sprintf($strRelationNotWorking, $url_to_goto, '</a>') . "\n";
  39. }
  40.  
  41.  
  42. /**
  43.  * Gets the "fpdf" libraries and defines the pdf font path
  44.  */
  45. require('./libraries/fpdf/fpdf.php');
  46. // loic1: PHP3 compatibility
  47. // define('FPDF_FONTPATH', './libraries/fpdf/font/');
  48. $FPDF_font_path = './libraries/fpdf/font/';
  49.  
  50.  
  51. /**
  52.  * Emulates the "array_search" function with PHP < 4.0.5
  53.  */
  54. if (PMA_PHP_INT_VERSION < 40005) {
  55.     function array_search($needle, $haystack) {
  56.         $match         = FALSE;
  57.  
  58.         reset($haystack);
  59.         while (list($key, $value) = each($haystack)) {
  60.             if ($value == $needle) {
  61.                 $match = $key;
  62.             }
  63.         } // end while
  64.  
  65.         return $match;
  66.     } // end of the "array_search" function
  67. } // end if
  68.  
  69.  
  70.  
  71. /**
  72.  * Extends the "FPDF" class and prepares the work
  73.  *
  74.  * @access  public
  75.  *
  76.  * @see     FPDF
  77.  */
  78. class PMA_PDF extends FPDF
  79. {
  80.     /**
  81.      * Defines private properties
  82.      */
  83.     var $x_min;
  84.     var $y_min;
  85.     var $l_marg = 10;
  86.     var $t_marg = 10;
  87.     var $scale;
  88.     var $title;
  89.     var $PMA_links;
  90.     var $Outlines=array();
  91.     var $def_outlines;
  92.     var $Alias ;
  93.     var $widths;
  94.  
  95.     /**
  96.      * The PMA_PDF constructor
  97.      *
  98.      * This function just refers to the "FPDF" constructor: with PHP3 a class
  99.      * must have a constructor
  100.      *
  101.      * @param  string  The page orientation (p, portrait, l or landscape)
  102.      * @param  string  The unit for sizes (pt, mm, cm or in)
  103.      * @param  mixed   The page format (A3, A4, A5, letter, legal or an array
  104.      *                 with page sizes)
  105.      *
  106.      * @access public
  107.      *
  108.      * @see     FPDF::FPDF()
  109.      */
  110.     function PMA_PDF($orientation = 'L', $unit = 'mm', $format = 'A4')
  111.     {
  112.         $this->Alias = array() ;
  113.         $this->FPDF($orientation, $unit, $format);
  114.     } // end of the "PMA_PDF()" method
  115.     function SetAlias($name, $value)
  116.     {
  117.         $this->Alias[$name] = $value ;
  118.     }
  119.     function _putpages()
  120.     {
  121.         if(count($this->Alias) > 0)
  122.         {
  123.             $nb=$this->page;
  124.             @reset($this->Alias);
  125.             while(list($alias, $value) = each($this->Alias)) {
  126.                 for($n=1;$n<=$nb;$n++)
  127.                 $this->pages[$n]=str_replace($alias,$value,$this->pages[$n]);
  128.             }
  129.         }
  130.         parent::_putpages();
  131.     }
  132.  
  133.     /**
  134.      * Sets the scaling factor, defines minimum coordinates and margins
  135.      *
  136.      * @param  double  The scaling factor
  137.      * @param  double  The minimum X coordinate
  138.      * @param  double  The minimum Y coordinate
  139.      * @param  double  The left margin
  140.      * @param  double  The top margin
  141.      *
  142.      * @access public
  143.      */
  144.     function PMA_PDF_setScale($scale = 1, $x_min = 0, $y_min = 0, $l_marg = -1, $t_marg = -1)
  145.     {
  146.         $this->scale      = $scale;
  147.         $this->x_min      = $x_min;
  148.         $this->y_min      = $y_min;
  149.         if ($this->l_marg != -1) {
  150.             $this->l_marg = $l_marg;
  151.         }
  152.         if ($this->t_marg != -1) {
  153.             $this->t_marg = $t_marg;
  154.         }
  155.     } // end of the "PMA_PDF_setScale" function
  156.  
  157.  
  158.     /**
  159.      * Outputs a scaled cell
  160.      *
  161.      * @param   double   The cell width
  162.      * @param   double   The cell height
  163.      * @param   string   The text to output
  164.      * @param   mixed    Wether to add borders or not
  165.      * @param   integer  Where to put the cursor once the output is done
  166.      * @param   string   Align mode
  167.      * @param   integer  Whether to fill the cell with a color or not
  168.      *
  169.      * @access public
  170.      *
  171.      * @see     FPDF::Cell()
  172.      */
  173.     function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0,$link ='')
  174.     {
  175.         $h = $h / $this->scale;
  176.         $w = $w / $this->scale;
  177.         $this->Cell($w, $h, $txt, $border, $ln, $align, $fill,$link);
  178.     } // end of the "PMA_PDF_cellScale" function
  179.  
  180.  
  181.     /**
  182.      * Draws a scaled line
  183.      *
  184.      * @param   double  The horizontal position of the starting point
  185.      * @param   double  The vertical position of the starting point
  186.      * @param   double  The horizontal position of the ending point
  187.      * @param   double  The vertical position of the ending point
  188.      *
  189.      * @access public
  190.      *
  191.      * @see     FPDF::Line()
  192.      */
  193.     function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
  194.     {
  195.         $x1 = ($x1 - $this->x_min) / $this->scale + $this->l_marg;
  196.         $y1 = ($y1 - $this->y_min) / $this->scale + $this->t_marg;
  197.         $x2 = ($x2 - $this->x_min) / $this->scale + $this->l_marg;
  198.         $y2 = ($y2 - $this->y_min) / $this->scale + $this->t_marg;
  199.         $this->Line($x1, $y1, $x2, $y2);
  200.     } // end of the "PMA_PDF_lineScale" function
  201.  
  202.  
  203.     /**
  204.      * Sets x and y scaled positions
  205.      *
  206.      * @param   double  The x position
  207.      * @param   double  The y position
  208.      *
  209.      * @access public
  210.      *
  211.      * @see     FPDF::SetXY()
  212.      */
  213.     function PMA_PDF_setXyScale($x, $y)
  214.     {
  215.         $x = ($x - $this->x_min) / $this->scale + $this->l_marg;
  216.         $y = ($y - $this->y_min) / $this->scale + $this->t_marg;
  217.         $this->SetXY($x, $y);
  218.     } // end of the "PMA_PDF_setXyScale" function
  219.  
  220.  
  221.     /**
  222.      * Sets the X scaled positions
  223.      *
  224.      * @param   double  The x position
  225.      *
  226.      * @access public
  227.      *
  228.      * @see     FPDF::SetX()
  229.      */
  230.     function PMA_PDF_setXScale($x)
  231.     {
  232.         $x = ($x - $this->x_min) / $this->scale + $this->l_marg;
  233.         $this->SetX($x);
  234.     } // end of the "PMA_PDF_setXScale" function
  235.  
  236.  
  237.     /**
  238.      * Sets the scaled font size
  239.      *
  240.      * @param   double   The font size (in points)
  241.      *
  242.      * @access public
  243.      *
  244.      * @see     FPDF::SetFontSize()
  245.      */
  246.     function PMA_PDF_setFontSizeScale($size)
  247.     {
  248.         // Set font size in points
  249.         $size = $size / $this->scale;
  250.         $this->SetFontSize($size);
  251.     } // end of the "PMA_PDF_setFontSizeScale" function
  252.  
  253.  
  254.     /**
  255.      * Sets the scaled line width
  256.      *
  257.      * @param   double  The line width
  258.      *
  259.      * @access public
  260.      *
  261.      * @see     FPDF::SetLineWidth()
  262.      */
  263.     function PMA_PDF_setLineWidthScale($width)
  264.     {
  265.         $width = $width / $this->scale;
  266.         $this->SetLineWidth($width);
  267.     } // end of the "PMA_PDF_setLineWidthScale" function
  268.  
  269.  
  270.     /**
  271.      * Displays an error message
  272.      *
  273.      * @param   string   the error mesage
  274.      *
  275.      * @global  array    the PMA configuration array
  276.      * @global  integer  the current server id
  277.      * @global  string   the current language
  278.      * @global  string   the charset to convert to
  279.      * @global  string   the current database name
  280.      * @global  string   the current charset
  281.      * @global  string   the current text direction
  282.      * @global  string   a localized string
  283.      * @global  string   an other localized string
  284.      *
  285.      * @access  public
  286.      */
  287.     function PMA_PDF_die($error_message = '')
  288.     {
  289.         global $cfg;
  290.         global $server, $lang, $convcharset, $db;
  291.         global $charset, $text_dir, $strRunning, $strDatabase;
  292.  
  293.         include('./header.inc.php');
  294.  
  295.         echo '<p><b>PDF - '. $GLOBALS['strError'] . '</b></p>' . "\n";
  296.         if (!empty($error_message)) {
  297.             $error_message = htmlspecialchars($error_message);
  298.         }
  299.         echo '<p>' . "\n";
  300.         echo '    ' . $error_message . "\n";
  301.         echo '</p>' . "\n";
  302.  
  303.         echo '<a href="db_details_structure.php?' . PMA_generate_common_url($db)
  304.              . '">' . $GLOBALS['strBack'] . '</a>';
  305.         echo "\n";
  306.  
  307.         include('./footer.inc.php');
  308.         exit();
  309.     } // end of the "PMA_PDF_die()" function
  310.  
  311.  
  312.     /**
  313.      * Aliases the "Error()" function from the FPDF class to the
  314.      * "PMA_PDF_die()" one
  315.      *
  316.      * @param   string   the error mesage
  317.      *
  318.      * @access  public
  319.      *
  320.      * @see     PMA_PDF_die()
  321.      */
  322.     function Error($error_message = '')
  323.     {
  324.         $this->PMA_PDF_die($error_message);
  325.     } // end of the "Error()" method
  326.  
  327.     function Header(){
  328.         //$datefmt
  329.         // We only show this if we find something in the new pdf_pages table
  330.         //
  331.         // This function must be named "Header" to work with the FPDF library
  332.  
  333.     global $cfgRelation,$db,$pdf_page_number,$with_doc;
  334.     if ($with_doc){
  335.         $test_query = 'SELECT * FROM ' . PMA_backquote($cfgRelation['pdf_pages'])
  336.                     . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  337.                     . ' AND page_nr = \'' . $pdf_page_number . '\'';
  338.         $test_rs    = PMA_query_as_cu($test_query);
  339.         $pages = @PMA_mysql_fetch_array($test_rs);
  340.         $this->SetFont('', 'B', 14);
  341.         $this->Cell(0,6, ucfirst($pages['page_descr']),'B',1,'C');
  342.         $this->SetFont('', '');
  343.         $this->Ln();
  344.     }
  345.     }
  346.     function Footer(){
  347.         // This function must be named "Footer" to work with the FPDF library
  348.         global $with_doc;
  349.         if ($with_doc){
  350.             $this->SetY(-15);
  351.             $this->SetFont('', '',14);
  352.             $this->Cell(0,6, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}','T',0,'C');
  353.             $this->Cell(0,6, PMA_localisedDate(),0,1,'R');
  354.             $this->SetY(20);
  355.         }
  356.     }
  357.     function Bookmark($txt,$level=0,$y=0)
  358. {
  359.    //Add a bookmark
  360.    $this->Outlines[0][]=$level;
  361.    $this->Outlines[1][]=$txt;
  362.    $this->Outlines[2][]=$this->page;
  363.    if($y==-1)
  364.       $y=$this->GetY();
  365.    $this->Outlines[3][]=round($this->hPt-$y*$this->k,2);
  366. }
  367.  
  368. function _putbookmarks()
  369. {
  370.    if(count($this->Outlines)>0)
  371.    {
  372.       //Save object number
  373.       $memo_n = $this->n;
  374.       //Take the number of sub elements for an outline
  375.       $nb_outlines=sizeof($this->Outlines[0]);
  376.       $first_level=array();
  377.       $parent=array();
  378.       $parent[0]=1;
  379.       for( $i=0; $i<$nb_outlines; $i++)
  380.       {
  381.          $level=$this->Outlines[0][$i];
  382.          $kids=0;
  383.          $last=-1;
  384.          $prev=-1;
  385.          $next=-1;
  386.          if( $i>0 )
  387.          {
  388.             $cursor=$i-1;
  389.             //Take the previous outline in the same level
  390.             while( $this->Outlines[0][$cursor] > $level && $cursor > 0)
  391.                $cursor--;
  392.             if( $this->Outlines[0][$cursor] == $level)
  393.                $prev=$cursor;
  394.          }
  395.          if( $i<$nb_outlines-1)
  396.          {
  397.             $cursor=$i+1;
  398.             while( isset($this->Outlines[0][$cursor]) && $this->Outlines[0][$cursor] > $level)
  399.             {
  400.                //Take the immediate kid in level + 1
  401.                if( $this->Outlines[0][$cursor] == $level+1)
  402.                {
  403.                   $kids++;
  404.                   $last=$cursor;
  405.                }
  406.                $cursor++;
  407.             }
  408.             $cursor=$i+1;
  409.             //Take the next outline in the same level
  410.             while( $this->Outlines[0][$cursor] > $level && ($cursor+1 < sizeof($this->Outlines[0])))
  411.                $cursor++;
  412.             if( $this->Outlines[0][$cursor] == $level)
  413.                $next=$cursor;
  414.          }
  415.          $this->_newobj();
  416.          $parent[$level+1]=$this->n;
  417.          if( $level == 0)
  418.             $first_level[]=$this->n;
  419.          $this->_out('<<');
  420.          $this->_out('/Title ('.$this->Outlines[1][$i].')');
  421.          $this->_out('/Parent '.$parent[$level].' 0 R');
  422.          if( $prev != -1)
  423.             $this->_out('/Prev '.($memo_n+$prev+1).' 0 R');
  424.          if( $next != -1)
  425.             $this->_out('/Next '.($this->n+$next-$i).' 0 R');
  426.          $this->_out('/Dest ['.(1+(2*$this->Outlines[2][$i])).' 0 R /XYZ null '.$this->Outlines[3][$i].' null]');
  427.          if( $kids > 0)
  428.          {
  429.             $this->_out('/First '.($this->n+1).' 0 R');
  430.             $this->_out('/Last '.($this->n+$last-$i).' 0 R');
  431.             $this->_out('/Count -'.$kids);
  432.          }
  433.          $this->_out('>>');
  434.          $this->_out('endobj');
  435.       }
  436.       //First page of outlines
  437.       $this->_newobj();
  438.       $this->def_outlines = $this->n;
  439.       $this->_out('<<');
  440.       $this->_out('/Type');
  441.       $this->_out('/Outlines');
  442.       $this->_out('/First '.$first_level[0].' 0 R');
  443.       $this->_out('/Last '.$first_level[sizeof($first_level)-1].' 0 R');
  444.       $this->_out('/Count '.sizeof($first_level));
  445.       $this->_out('>>');
  446.       $this->_out('endobj');
  447.    }
  448. }
  449.  
  450. function _putresources()
  451. {
  452.    parent::_putresources();
  453.    $this->_putbookmarks();
  454. }
  455.  
  456. function _putcatalog()
  457. {
  458.    parent::_putcatalog();
  459.    if(count($this->Outlines)>0)
  460.    {
  461.       $this->_out('/Outlines '.$this->def_outlines.' 0 R');
  462.       $this->_out('/PageMode /UseOutlines');
  463.    }
  464. }
  465. function SetWidths($w)
  466. {
  467.    // column widths
  468.    $this->widths=$w;
  469. }
  470.  
  471. function Row($data,$links)
  472. {
  473.    // line height
  474.    $nb=0;
  475.    for($i=0;$i<count($data);$i++)
  476.       $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
  477.    $il = $this->FontSize;
  478.    $h=($il+1)*$nb;
  479.    // page break if necessary
  480.    $this->CheckPageBreak($h);
  481.    // draw the cells
  482.    for($i=0;$i<count($data);$i++)
  483.    {
  484.       $w=$this->widths[$i];
  485.       // save current position
  486.       $x=$this->GetX();
  487.       $y=$this->GetY();
  488.       // draw the border
  489.       $this->Rect($x,$y,$w,$h);
  490.       if (isset($links[$i]))
  491.       $this->Link($x,$y,$w,$h,$links[$i]);
  492.       // print text
  493.       $this->MultiCell($w,$il+1,$data[$i],0,'L');
  494.       // go to right side
  495.       $this->SetXY($x+$w,$y);
  496.    }
  497.    // go to line
  498.    $this->Ln($h);
  499. }
  500.  
  501. function CheckPageBreak($h)
  502. {
  503.    // if height h overflows, manual page break
  504.    if($this->GetY()+$h>$this->PageBreakTrigger)
  505.       $this->AddPage($this->CurOrientation);
  506. }
  507.  
  508. function NbLines($w,$txt)
  509. {
  510.    // compute number of lines used by a multicell of width w
  511.    $cw=&$this->CurrentFont['cw'];
  512.    if($w==0)
  513.       $w=$this->w-$this->rMargin-$this->x;
  514.    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  515.    $s=str_replace("\r",'',$txt);
  516.    $nb=strlen($s);
  517.    if($nb>0 and $s[$nb-1]=="\n")
  518.       $nb--;
  519.    $sep=-1;
  520.    $i=0;
  521.    $j=0;
  522.    $l=0;
  523.    $nl=1;
  524.    while($i<$nb)
  525.    {
  526.       $c=$s[$i];
  527.       if($c=="\n")
  528.       {
  529.          $i++;
  530.          $sep=-1;
  531.          $j=$i;
  532.          $l=0;
  533.          $nl++;
  534.          continue;
  535.       }
  536.       if($c==' ')
  537.          $sep=$i;
  538.       $l+=$cw[$c];
  539.       if($l>$wmax)
  540.       {
  541.          if($sep==-1)
  542.          {
  543.             if($i==$j)
  544.                $i++;
  545.          }
  546.          else
  547.             $i=$sep+1;
  548.          $sep=-1;
  549.          $j=$i;
  550.          $l=0;
  551.          $nl++;
  552.       }
  553.       else
  554.          $i++;
  555.    }
  556.    return $nl;
  557. }
  558.  
  559. } // end of the "PMA_PDF" class
  560.  
  561.  
  562. /**
  563.  * Draws tables schema
  564.  *
  565.  * @access  private
  566.  *
  567.  * @see     PMA_RT
  568.  */
  569. class PMA_RT_Table
  570. {
  571.     /**
  572.      * Defines private properties
  573.      */
  574.     var $nb_fiels;
  575.     var $table_name;
  576.     var $width = 0;
  577.     var $height;
  578.     var $fields      = array();
  579.     var $height_cell = 6;
  580.     var $x, $y;
  581.     var $primary     = array();
  582.  
  583.  
  584.     /**
  585.      * Sets the width of the table
  586.      *
  587.      * @param   integer   The font size
  588.      *
  589.      * @global  object    The current PDF document
  590.      *
  591.      * @access  private
  592.      *
  593.      * @see     PMA_PDF
  594.      */
  595.     function PMA_RT_Table_setWidth($ff)
  596.     {
  597.         //  this looks buggy to me... does it really work if
  598.         //  there are fields that require wider cells than the name of the table?
  599.         global $pdf;
  600.  
  601.         reset($this->fields);
  602.         while (list(, $field) = each($this->fields)) {
  603.             $this->width = max($this->width, $pdf->GetStringWidth($field));
  604.         }
  605.         $this->width += $pdf->GetStringWidth('  ');
  606.         $pdf->SetFont($ff, 'B');
  607.         $this->width = max($this->width, $pdf->GetStringWidth('  ' . $this->table_name));
  608.         $pdf->SetFont($ff, '');
  609.     } // end of the "PMA_RT_Table_setWidth()" method
  610.  
  611.  
  612.     /**
  613.      * Sets the height of the table
  614.      *
  615.      * @access  private
  616.      */
  617.     function PMA_RT_Table_setHeight()
  618.     {
  619.         $this->height = (count($this->fields) + 1) * $this->height_cell;
  620.     } // end of the "PMA_RT_Table_setHeight()" method
  621.  
  622.  
  623.     /**
  624.      * Do draw the table
  625.      *
  626.      * @param   boolean   Whether to display table position or not
  627.      * @param   integer   The font size
  628.      * @param   boolean   Whether all tables should have the same width or not
  629.      * @param   integer   The max. with among tables
  630.      *
  631.      * @global  object    The current PDF document
  632.      *
  633.      * @access  private
  634.      *
  635.      * @see     PMA_PDF
  636.      */
  637.     function PMA_RT_Table_draw($show_info, $ff)
  638.     {
  639.         global $pdf, $with_doc;
  640.  
  641.         $pdf->PMA_PDF_setXyScale($this->x, $this->y);
  642.         $pdf->SetFont($ff, 'B');
  643.         $pdf->SetTextColor(200);
  644.         $pdf->SetFillColor(0, 0, 128);
  645.         if ($with_doc) $pdf->SetLink($pdf->PMA_links['RT'][$this->table_name]['-'],-1);
  646.         else $pdf->PMA_links['doc'][$this->table_name]['-'] = '';
  647.         if ($show_info){
  648.             $pdf->PMA_PDF_cellScale($this->width, $this->height_cell, sprintf('%.0f', $this->width) . 'x' . sprintf('%.0f', $this->height) . ' ' . $this->table_name, 1, 1, 'C', 1,$pdf->PMA_links['doc'][$this->table_name]['-']);
  649.         } else {
  650.             $pdf->PMA_PDF_cellScale($this->width, $this->height_cell, $this->table_name, 1, 1, 'C', 1,$pdf->PMA_links['doc'][$this->table_name]['-']);
  651.         }
  652.         $pdf->PMA_PDF_setXScale($this->x);
  653.         $pdf->SetFont($ff, '');
  654.         $pdf->SetTextColor(0);
  655.         $pdf->SetFillColor(255);
  656.  
  657.         reset($this->fields);
  658.         while (list(, $field) = each($this->fields)) {
  659.             // loic1 : PHP3 fix
  660.             // if (in_array($field, $this->primary)) {
  661.             if (PMA_isInto($field, $this->primary) != -1) {
  662.                 $pdf->SetFillColor(215, 121, 123);
  663.             }
  664.             if ($field == $this->displayfield) {
  665.                 $pdf->SetFillColor(142, 159, 224);
  666.             }
  667.             if ($with_doc) $pdf->SetLink($pdf->PMA_links['RT'][$this->table_name][$field],-1);
  668.             else $pdf->PMA_links['doc'][$this->table_name][$field] = '';
  669.  
  670.  
  671.             $pdf->PMA_PDF_cellScale($this->width, $this->height_cell, ' ' . $field, 1, 1, 'L', 1,$pdf->PMA_links['doc'][$this->table_name][$field]);
  672.             $pdf->PMA_PDF_setXScale($this->x);
  673.             $pdf->SetFillColor(255);
  674.         } // end while
  675.  
  676.         /*if ($pdf->PageNo() > 1) {
  677.             $pdf->PMA_PDF_die($GLOBALS['strScaleFactorSmall']);
  678.         } */
  679.     } // end of the "PMA_RT_Table_draw()" method
  680.  
  681.  
  682.     /**
  683.      * The "PMA_RT_Table" constructor
  684.      *
  685.      * @param   string    The table name
  686.      * @param   integer   The font size
  687.      * @param   integer   The max. with among tables
  688.      *
  689.      * @global  object    The current PDF document
  690.      * @global  integer   The current page number (from the
  691.      *                    $cfg['Servers'][$i]['table_coords'] table)
  692.      * @global  array     The relations settings
  693.      * @global  string    The current db name
  694.      *
  695.      * @access  private
  696.      *
  697.      * @see     PMA_PDF, PMA_RT_Table::PMA_RT_Table_setWidth,
  698.      *          PMA_RT_Table::PMA_RT_Table_setHeight
  699.      */
  700.     function PMA_RT_Table($table_name, $ff, &$same_wide_width)
  701.     {
  702.         global $pdf, $pdf_page_number, $cfgRelation, $db;
  703.  
  704.         $this->table_name = $table_name;
  705.         $sql              = 'DESCRIBE ' .  PMA_backquote($table_name);
  706.         $result           = PMA_mysql_query($sql);
  707.         if (!$result || !mysql_num_rows($result)) {
  708.             $pdf->PMA_PDF_die(sprintf($GLOBALS['strPdfInvalidTblName'], $table_name));
  709.         }
  710.         // load fields
  711.         while ($row = PMA_mysql_fetch_array($result)) {
  712.             $this->fields[] = $row[0];
  713.         }
  714.  
  715.         //height and width
  716.         $this->PMA_RT_Table_setWidth($ff);
  717.         $this->PMA_RT_Table_setHeight();
  718.         if ($same_wide_width < $this->width) {
  719.             $same_wide_width = $this->width;
  720.         }
  721.  
  722.         //x and y
  723.         $sql    = 'SELECT x, y FROM '
  724.                 . PMA_backquote($cfgRelation['table_coords'])
  725.                 .   ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  726.                 .   ' AND   table_name = \'' . PMA_sqlAddslashes($table_name) . '\''
  727.                 .   ' AND   pdf_page_number = ' . $pdf_page_number;
  728.         $result = PMA_query_as_cu($sql);
  729.  
  730.         if (!$result || !mysql_num_rows($result)) {
  731.             $pdf->PMA_PDF_die(sprintf($GLOBALS['strConfigureTableCoord'], $table_name));
  732.         }
  733.         list($this->x, $this->y) = PMA_mysql_fetch_array($result);
  734.         $this->x = (double) $this->x;
  735.         $this->y = (double) $this->y;
  736.         // displayfield
  737.         $this->displayfield = PMA_getDisplayField($db, $table_name);
  738.  
  739.         // index
  740.         $sql    = 'SHOW index FROM ' . PMA_backquote($table_name);
  741.         $result = PMA_mysql_query($sql);
  742.         if ($result && mysql_num_rows($result) > 0) {
  743.             while ($row = PMA_mysql_fetch_array($result)) {
  744.                 if ($row['Key_name'] == 'PRIMARY') {
  745.                     $this->primary[] = $row['Column_name'];
  746.                 }
  747.             }
  748.         } // end if
  749.     } // end of the "PMA_RT_Table()" method
  750. } // end class "PMA_RT_Table"
  751.  
  752.  
  753.  
  754. /**
  755.  * Draws relation links
  756.  *
  757.  * @access  private
  758.  *
  759.  * @see     PMA_RT
  760.  */
  761. class PMA_RT_Relation
  762. {
  763.     /**
  764.      * Defines private properties
  765.      */
  766.     var $x_src, $y_src;
  767.     var $src_dir ;
  768.     var $dest_dir;
  769.     var $x_dest, $y_dest;
  770.     var $w_tick = 5;
  771.  
  772.  
  773.     /**
  774.      * Gets arrows coordinates
  775.      *
  776.      * @param   string    The current table name
  777.      * @param   string    The relation column name
  778.      *
  779.      * @return  array     Arrows coordinates
  780.      *
  781.      * @access  private
  782.      */
  783.     function PMA_RT_Relation_getXy($table, $column)
  784.     {
  785.         $pos = array_search($column, $table->fields);
  786.         // x_left, x_right, y
  787.         return array($table->x, $table->x + + $table->width, $table->y + ($pos + 1.5) * $table->height_cell);
  788.     } // end of the "PMA_RT_Relation_getXy()" method
  789.  
  790.  
  791.     /**
  792.      * Do draws relation links
  793.      *
  794.      * @param   boolean   Whether to use one color per relation or not
  795.      * @param   integer   The id of the link to draw
  796.      *
  797.      * @global  object    The current PDF document
  798.      *
  799.      * @access  private
  800.      *
  801.      * @see     PMA_PDF
  802.      */
  803.     function PMA_RT_Relation_draw($change_color, $i)
  804.     {
  805.         global $pdf;
  806.  
  807.         if ($change_color){
  808.             $d    = $i % 6;
  809.             $j    = ($i - $d) / 6;
  810.             $j    = $j % 4;
  811.             $j++;
  812.             $case = array(
  813.                         array(1, 0, 0),
  814.                         array(0, 1, 0),
  815.                         array(0, 0, 1),
  816.                         array(1, 1, 0),
  817.                         array(1, 0, 1),
  818.                         array(0, 1, 1)
  819.                     );
  820.             list ($a, $b, $c) = $case[$d];
  821.             $e    = (1 - ($j - 1) / 6);
  822.             $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);       }
  823.         else {
  824.             $pdf->SetDrawColor(0);
  825.         } // end if... else...
  826.  
  827.         $pdf->PMA_PDF_setLineWidthScale(0.2);
  828.         $pdf->PMA_PDF_lineScale($this->x_src, $this->y_src, $this->x_src + $this->src_dir * $this->w_tick, $this->y_src);
  829.         $pdf->PMA_PDF_lineScale($this->x_dest + $this->dest_dir * $this->w_tick, $this->y_dest, $this->x_dest, $this->y_dest);
  830.         $pdf->PMA_PDF_setLineWidthScale(0.1);
  831.         $pdf->PMA_PDF_lineScale($this->x_src + $this->src_dir * $this->w_tick, $this->y_src, $this->x_dest + $this->dest_dir * $this->w_tick, $this->y_dest);
  832.  
  833.         //arrow
  834.         $root2 = 2 * sqrt(2);
  835.         $pdf->PMA_PDF_lineScale($this->x_src + $this->src_dir * $this->w_tick * 0.75, $this->y_src, $this->x_src + $this->src_dir * (0.75 - 1 / $root2) * $this->w_tick, $this->y_src + $this->w_tick / $root2);
  836.         $pdf->PMA_PDF_lineScale($this->x_src + $this->src_dir * $this->w_tick * 0.75, $this->y_src, $this->x_src + $this->src_dir * (0.75 - 1 / $root2) * $this->w_tick, $this->y_src - $this->w_tick / $root2);
  837.  
  838.         $pdf->PMA_PDF_lineScale($this->x_dest + $this->dest_dir * $this->w_tick / 2, $this->y_dest, $this->x_dest + $this->dest_dir * (0.5 + 1 / $root2) * $this->w_tick, $this->y_dest + $this->w_tick / $root2);
  839.         $pdf->PMA_PDF_lineScale($this->x_dest + $this->dest_dir * $this->w_tick / 2, $this->y_dest, $this->x_dest + $this->dest_dir * (0.5 + 1 / $root2) * $this->w_tick, $this->y_dest - $this->w_tick / $root2);
  840.         $pdf->SetDrawColor(0);
  841.     } // end of the "PMA_RT_Relation_draw()" method
  842.  
  843.  
  844.     /**
  845.      * The "PMA_RT_Relation" constructor
  846.      *
  847.      * @param   string   The master table name
  848.      * @param   string   The relation field in the master table
  849.      * @param   string   The foreign table name
  850.      * @param   string   The relation field in the foreign table
  851.      *
  852.      *
  853.      * @access  private
  854.      *
  855.      * @see     PMA_RT_Relation::PMA_RT_Relation_getXy
  856.      */
  857.     function PMA_RT_Relation($master_table, $master_field,  $foreign_table, $foreign_field)
  858.     {
  859.         $src_pos    = $this->PMA_RT_Relation_getXy($master_table , $master_field);
  860.         $dest_pos   = $this->PMA_RT_Relation_getXy($foreign_table, $foreign_field);
  861.         $src_left   = $src_pos[0] - $this->w_tick;
  862.         $src_right  = $src_pos[1] + $this->w_tick;
  863.         $dest_left  = $dest_pos[0] - $this->w_tick;
  864.         $dest_right = $dest_pos[1] + $this->w_tick;
  865.  
  866.         $d1 = abs($src_left  - $dest_left);
  867.         $d2 = abs($src_right - $dest_left);
  868.         $d3 = abs($src_left  - $dest_right);
  869.         $d4 = abs($src_right - $dest_right);
  870.         $d  = min($d1, $d2, $d3, $d4);
  871.  
  872.         if ($d == $d1) {
  873.             $this->x_src    = $src_pos[0];
  874.             $this->src_dir  = -1;
  875.             $this->x_dest   = $dest_pos[0];
  876.             $this->dest_dir = -1;
  877.         } else if ($d == $d2) {
  878.             $this->x_src    = $src_pos[1];
  879.             $this->src_dir  = 1;
  880.             $this->x_dest   = $dest_pos[0];
  881.             $this->dest_dir = -1;
  882.         } else if ($d == $d3) {
  883.             $this->x_src    = $src_pos[0];
  884.             $this->src_dir  = -1;
  885.             $this->x_dest   = $dest_pos[1];
  886.             $this->dest_dir = 1;
  887.         } else {
  888.             $this->x_src    =  $src_pos[1];
  889.             $this->src_dir  = 1;
  890.             $this->x_dest   = $dest_pos[1];
  891.             $this->dest_dir = 1;
  892.         }
  893.         $this->y_src        = $src_pos[2];
  894.         $this->y_dest       = $dest_pos[2];
  895.     } // end of the "PMA_RT_Relation()" method
  896. } // end of the "PMA_RT_Relation" class
  897.  
  898.  
  899.  
  900. /**
  901.  * Draws and send the database schema
  902.  *
  903.  * @access  public
  904.  *
  905.  * @see     PMA_PDF
  906.  */
  907. class PMA_RT
  908. {
  909.     /**
  910.      * Defines private properties
  911.      */
  912.     var $tables    = array();
  913.     var $relations = array();
  914.     var $ff        = 'Arial';
  915.     var $x_max     = 0;
  916.     var $y_max     = 0;
  917.     var $scale;
  918.     var $x_min     = 100000;
  919.     var $y_min     = 100000;
  920.     var $t_marg    = 10;
  921.     var $b_marg    = 10;
  922.     var $l_marg    = 10;
  923.     var $r_marg    = 10;
  924.     var $tablewidth;
  925.     var $same_wide = 0;
  926.  
  927.     /**
  928.      * Sets X and Y minimum and maximum for a table cell
  929.      *
  930.      * @param   string   The table name
  931.      *
  932.      * @access  private
  933.      */
  934.     function PMA_RT_setMinMax($table)
  935.     {
  936.         $this->x_max = max($this->x_max, $table->x + $table->width);
  937.         $this->y_max = max($this->y_max, $table->y + $table->height);
  938.         $this->x_min = min($this->x_min, $table->x);
  939.         $this->y_min = min($this->y_min, $table->y);
  940.     } // end of the "PMA_RT_setMinMax()" method
  941.  
  942.  
  943.     /**
  944.      * Defines relation objects
  945.      *
  946.      * @param   string   The master table name
  947.      * @param   string   The relation field in the master table
  948.      * @param   string   The foreign table name
  949.      * @param   string   The relation field in the foreign table
  950.      *
  951.      * @access  private
  952.      *
  953.      * @see     PMA_RT_setMinMax()
  954.      */
  955.     function PMA_RT_addRelation($master_table , $master_field,  $foreign_table, $foreign_field)
  956.     {
  957.         if (!isset($this->tables[$master_table])) {
  958.             $this->tables[$master_table] = new PMA_RT_Table($master_table, $this->ff, $this->tablewidth);
  959.             $this->PMA_RT_setMinMax($this->tables[$master_table]);
  960.         }
  961.         if (!isset($this->tables[$foreign_table])) {
  962.             $this->tables[$foreign_table] = new PMA_RT_Table($foreign_table, $this->ff, $this->tablewidth);
  963.             $this->PMA_RT_setMinMax($this->tables[$foreign_table]);
  964.         }
  965.         $this->relations[] = new PMA_RT_Relation($this->tables[$master_table], $master_field, $this->tables[$foreign_table], $foreign_field);
  966.     } // end of the "PMA_RT_addRelation()" method
  967.  
  968.  
  969.     /**
  970.      * Draws the grid
  971.      *
  972.      * @global  object  the current PMA_PDF instance
  973.      *
  974.      * @access  private
  975.      *
  976.      * @see     PMA_PDF
  977.      */
  978.     function PMA_RT_strokeGrid()
  979.     {
  980.         global $pdf;
  981.  
  982.         $pdf->SetMargins(0, 0);
  983.         $pdf->SetDrawColor(200, 200, 200);
  984.  
  985.         // Draws horizontal lines
  986.         for ($l = 0; $l < 21; $l++) {
  987.             $pdf->line(0, $l * 10, $pdf->fh, $l * 10);
  988.             // Avoid duplicates
  989.             if ($l > 0) {
  990.                 $pdf->SetXY(0, $l * 10);
  991.                 $label = (string) sprintf('%.0f', ($l * 10 - $this->t_marg) * $this->scale + $this->y_min);
  992.                 $pdf->Cell(5, 5, ' ' . $label);
  993.             } // end if
  994.         } // end for
  995.  
  996.         // Draws vertical lines
  997.         for ($j = 0; $j < 30 ;$j++) {
  998.             $pdf->line($j * 10, 0, $j * 10, $pdf->fw);
  999.             $pdf->SetXY($j * 10, 0);
  1000.             $label = (string) sprintf('%.0f', ($j * 10 - $this->l_marg) * $this->scale + $this->x_min);
  1001.             $pdf->Cell(5, 7, $label);
  1002.         } // end for
  1003.     } // end of the "PMA_RT_strokeGrid()" method
  1004.  
  1005.  
  1006.     /**
  1007.      * Draws relation arrows
  1008.      *
  1009.      * @param   boolean  Whether to use one color per relation or not
  1010.      *
  1011.      * @access  private
  1012.      *
  1013.      * @see     PMA_RT_Relation::PMA_RT_Relation_draw()
  1014.      */
  1015.     function PMA_RT_drawRelations($change_color)
  1016.     {
  1017.         $i = 0;
  1018.         reset($this->relations);
  1019.         while (list(, $relation) = each($this->relations)) {
  1020.             $relation->PMA_RT_Relation_draw($change_color, $i);
  1021.             $i++;
  1022.         } // end while
  1023.     } // end of the "PMA_RT_drawRelations()" method
  1024.  
  1025.  
  1026.     /**
  1027.      * Draws tables
  1028.      *
  1029.      * @param   boolean  Whether to display table position or not
  1030.      *
  1031.      * @access  private
  1032.      *
  1033.      * @see     PMA_RT_Table::PMA_RT_Table_draw()
  1034.      */
  1035.     function PMA_RT_drawTables($show_info)
  1036.     {
  1037.         reset($this->tables);
  1038.         while (list(, $table) = each($this->tables)) {
  1039.             $table->PMA_RT_Table_draw($show_info, $this->ff);
  1040.         }
  1041.     } // end of the "PMA_RT_drawTables()" method
  1042.  
  1043.  
  1044.     /**
  1045.      * Ouputs the PDF document to a file
  1046.      *
  1047.      * @global  object   The current PDF document
  1048.      * @global  string   The current database name
  1049.      * @global  integer  The current page number (from the
  1050.      *                   $cfg['Servers'][$i]['table_coords'] table)
  1051.      *
  1052.      * @access  private
  1053.      *
  1054.      * @see     PMA_PDF
  1055.      */
  1056.     function PMA_RT_showRt()
  1057.     {
  1058.         global $pdf, $db, $pdf_page_number, $cfgRelation;
  1059.  
  1060.         $pdf->SetFontSize(14);
  1061.         $pdf->SetLineWidth(0.2);
  1062.         $pdf->SetDisplayMode('fullpage');
  1063.         //  Get the name of this pdfpage to use as filename (Mike Beck)
  1064.         $_name_sql  = 'SELECT page_descr FROM ' . PMA_backquote($cfgRelation['pdf_pages'])
  1065.                   .   ' WHERE page_nr = ' . $pdf_page_number;
  1066.         $_name_rs   = PMA_query_as_cu($_name_sql);
  1067.         if ($_name_rs) {
  1068.             $_name_row = PMA_mysql_fetch_row($_name_rs);
  1069.             $filename = $_name_row[0] . '.pdf';
  1070.         }
  1071.         // i don't know if there is a chance for this to happen, but rather be on the safe side:
  1072.         if (empty($filename)) {
  1073.             $filename = $pdf_page_number . '.pdf';
  1074.         }
  1075.         $pdf->Output($db . '_' . $filename, TRUE);
  1076.         //$pdf->Output('', TRUE);
  1077.     } // end of the "PMA_RT_showRt()" method
  1078.  
  1079.  
  1080.     /**
  1081.      * The "PMA_RT" constructor
  1082.      *
  1083.      * @param   mixed    The scaling factor
  1084.      * @param   integer  The page number to draw (from the
  1085.      *                   $cfg['Servers'][$i]['table_coords'] table)
  1086.      * @param   boolean  Whether to display table position or not
  1087.      * @param   boolean  Whether to use one color per relation or not
  1088.      * @param   boolean  Whether to draw grids or not
  1089.      * @param   boolean  Whether all tables should have the same width or not
  1090.      *
  1091.      * @global  object   The current PDF document
  1092.      * @global  string   The current db name
  1093.      * @global  array    The relations settings
  1094.      *
  1095.      * @access  private
  1096.      *
  1097.      * @see     PMA_PDF
  1098.      */
  1099.     function PMA_RT( $which_rel, $show_info = 0, $change_color = 0 , $show_grid = 0, $all_tab_same_wide = 0, $orientation = 'L', $paper = 'A4')
  1100.     {
  1101.         global $pdf, $db, $cfgRelation, $with_doc;
  1102.  
  1103.         // Font face depends on the current language
  1104.         $this->ff        = str_replace('"', '', substr($GLOBALS['right_font_family'], 0, strpos($GLOBALS['right_font_family'], ',')));
  1105.         $this->same_wide = $all_tab_same_wide;
  1106.  
  1107.         // Initializes a new document
  1108.         $pdf          = new PMA_PDF('L', 'mm', $paper);
  1109.         $pdf->title   = sprintf($GLOBALS['strPdfDbSchema'], $GLOBALS['db'], $which_rel);
  1110.         $pdf->cMargin = 0;
  1111.         $pdf->Open();
  1112.         $pdf->SetTitle($pdf->title);
  1113.         $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION);
  1114.         $pdf->AliasNbPages();
  1115.  
  1116.          // fonts added to phpMyAdmin and considered non-standard by fpdf
  1117.          // (Note: those tahoma fonts are iso-8859-2 based)
  1118.          if ($this->ff == 'tahoma') {
  1119.              $pdf->AddFont('tahoma','','tahoma.php');
  1120.              $pdf->AddFont('tahoma','B','tahomab.php');
  1121.          }
  1122.  
  1123.         $pdf->SetFont($this->ff, '', 14);
  1124.         $pdf->SetAutoPageBreak('auto');
  1125.  
  1126.         // Gets tables on this page
  1127.         $tab_sql  = 'SELECT table_name FROM ' . PMA_backquote($cfgRelation['table_coords'])
  1128.                   .   ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  1129.                   .   ' AND pdf_page_number = ' . $which_rel;
  1130.         $tab_rs   = PMA_query_as_cu($tab_sql);
  1131.         if (!$tab_rs || !mysql_num_rows($tab_rs) > 0) {
  1132.             $pdf->PMA_PDF_die($GLOBALS['strPdfNoTables']);
  1133. //            die('No tables');
  1134.         }
  1135.         while ($curr_table = @PMA_mysql_fetch_array($tab_rs)) {
  1136.             $alltables[] = PMA_sqlAddslashes($curr_table['table_name']);
  1137.             //$intable     = '\'' . implode('\', \'', $alltables) . '\'';
  1138.         }
  1139.  
  1140.         //              make doc                    //
  1141.         if ($with_doc) {
  1142.             $pdf->SetAutoPageBreak('auto',15);
  1143.             $pdf->cMargin = 1;
  1144.             PMA_RT_DOC($alltables);
  1145.             $pdf->SetAutoPageBreak('auto');
  1146.             $pdf->cMargin = 0;
  1147.         }
  1148.  
  1149.         $pdf->Addpage();
  1150.  
  1151.  
  1152.         if ($with_doc) {
  1153.             $pdf->SetLink($pdf->PMA_links['RT']['-'],-1);
  1154.             $pdf->Bookmark($GLOBALS['strRelationalSchema']);
  1155.             $pdf->SetAlias('{00}', $pdf->PageNo()) ;
  1156.             $this->t_marg = 18;
  1157.             $this->b_marg = 18;
  1158.         }
  1159.  
  1160.                                 /* snip */
  1161.  
  1162.         reset ($alltables);
  1163.         while (list(, $table) = each ($alltables)) {
  1164.             if (!isset($this->tables[$table])) {
  1165.                 $this->tables[$table] = new PMA_RT_Table($table, $this->ff, $this->tablewidth);
  1166.             }
  1167.         } // while
  1168.         reset($alltables);
  1169.         while (list(, $table) = each ($alltables)) {
  1170.             if($this->same_wide){
  1171.                 $this->tables[$table]->width = $this->tablewidth;
  1172.             }
  1173.             $this->PMA_RT_setMinMax($this->tables[$table]);
  1174.         }
  1175.         // Defines the scale factor
  1176.         $this->scale = ceil(max(($this->x_max - $this->x_min) / ($pdf->fh - $this->r_marg - $this->l_marg), ($this->y_max - $this->y_min) / ($pdf->fw - $this->t_marg - $this->b_marg)) * 100) / 100;
  1177.         $pdf->PMA_PDF_setScale($this->scale, $this->x_min, $this->y_min, $this->l_marg, $this->t_marg);
  1178.  
  1179.  
  1180.         // Builds and save the PDF document
  1181.         $pdf->PMA_PDF_setLineWidthScale(0.1);
  1182.  
  1183.         if ($show_grid) {
  1184.             $pdf->SetFontSize(10);
  1185.             $this->PMA_RT_strokeGrid();
  1186.         }
  1187.         $pdf->PMA_PDF_setFontSizeScale(14);
  1188.  
  1189.  
  1190. //        $sql    = 'SELECT * FROM ' . PMA_backquote($cfgRelation['relation'])
  1191. //                .   ' WHERE master_db   = \'' . PMA_sqlAddslashes($db) . '\' '
  1192. //                .   ' AND foreign_db    = \'' . PMA_sqlAddslashes($db) . '\' '
  1193. //                .   ' AND master_table  IN (' . $intable . ')'
  1194. //                .   ' AND foreign_table IN (' . $intable . ')';
  1195. //        $result =  PMA_query_as_cu($sql);
  1196. //
  1197. // lem9: 
  1198. // previous logic was checking master tables and foreign tables
  1199. // but I think that looping on every table of the pdf page as a master
  1200. // and finding its foreigns is OK (then we can support innodb)
  1201.  
  1202.         $seen_a_relation = FALSE;
  1203.         reset($alltables);
  1204.         while (list(,$one_table) = each($alltables)) {
  1205.  
  1206.             $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
  1207.             if ($exist_rel) {
  1208.                 $seen_a_relation = TRUE;
  1209.                 while (list($master_field,$rel) = each($exist_rel)) {
  1210.                     // put the foreign table on the schema only if selected
  1211.                     // by the user 
  1212.                     // (do not use array_search() because we would have to
  1213.                     // to do a === FALSE and this is not PHP3 compatible)
  1214.  
  1215.                     if (PMA_isInto($rel['foreign_table'], $alltables)> -1) {
  1216.                         $this->PMA_RT_addRelation($one_table , $master_field, $rel['foreign_table'], $rel['foreign_field']);
  1217.                     }
  1218.  
  1219.                 } // end while
  1220.             } // end if
  1221.         } // end while
  1222.  
  1223.         // loic1: also show tables without relations
  1224. //        $norelations     = TRUE;
  1225. //        if ($result && mysql_num_rows($result) > 0) {
  1226. //            $norelations = FALSE;
  1227. //            while ($row = PMA_mysql_fetch_array($result)) {
  1228. //                $this->PMA_RT_addRelation($row['master_table'] , $row['master_field'], $row['foreign_table'], $row['foreign_field']);
  1229. //            }
  1230. //        }
  1231.  
  1232.  
  1233. //        if ($norelations == FALSE) {
  1234.         if ($seen_a_relation) {
  1235.             $this->PMA_RT_drawRelations($change_color);
  1236.         }
  1237.  
  1238.         $this->PMA_RT_drawTables($show_info);
  1239.  
  1240.         $this->PMA_RT_showRt();
  1241.     } // end of the "PMA_RT()" method
  1242. } // end of the "PMA_RT" class
  1243.  
  1244. function PMA_RT_DOC($alltables ){
  1245.     global  $db, $pdf, $orientation;
  1246.     //TOC
  1247.     $pdf->addpage("P");
  1248.     $pdf->Cell(0,9, $GLOBALS['strTableOfContents'],1,0,'C');
  1249.     $pdf->Ln(15);
  1250.     $i = 1;
  1251.     @reset($alltables);
  1252.     while(list(, $table) = each($alltables)) {
  1253.         $pdf->PMA_links['doc'][$table]['-'] = $pdf->AddLink();
  1254.         $pdf->SetX(10);
  1255.         //$pdf->Ln(1);
  1256.         $pdf->Cell(0,6,$GLOBALS['strPageNumber'] . ' {'.sprintf("%02d", $i).'}',0,0,'R',0,$pdf->PMA_links['doc'][$table]['-']);
  1257.         $pdf->SetX(10);
  1258.         $pdf->Cell(0,6,$i.' '. $table,0,1,'L',0,$pdf->PMA_links['doc'][$table]['-']);
  1259.  
  1260.         //$pdf->Ln(1);
  1261.         $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table);
  1262.         $result      = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
  1263.         while ($row = PMA_mysql_fetch_array($result)) {
  1264.             $pdf->SetX(20);
  1265.             $field_name = $row['Field'];
  1266.             $pdf->PMA_links['doc'][$table][$field_name] =$pdf->AddLink();
  1267.             //$pdf->Cell(0,6,$field_name,0,1,'L',0,$pdf->PMA_links['doc'][$table][$field_name]);
  1268.         }
  1269.         $lasttable = $table;
  1270.         $i++;
  1271.     }
  1272.     $pdf->PMA_links['RT']['-'] =$pdf->AddLink();
  1273.     $pdf->SetX(10);
  1274.     $pdf->Cell(0,6,$GLOBALS['strPageNumber'] . ' {00}',0,0,'R',0,$pdf->PMA_links['doc'][$lasttable]['-']);
  1275.     $pdf->SetX(10);
  1276.     $pdf->Cell(0,6,$i.' '. $GLOBALS['strRelationalSchema'],0,1,'L',0,$pdf->PMA_links['RT']['-']);
  1277.     $z = 0;
  1278.     @reset($alltables);
  1279.     while(list(, $table) = each($alltables)) {
  1280.         $z++;
  1281.         $pdf->addpage($GLOBALS['orientation']);
  1282.         $pdf->Bookmark($table);
  1283.         $pdf->SetAlias('{'.sprintf("%02d", $z).'}', $pdf->PageNo()) ;
  1284.         $pdf->PMA_links['RT'][$table]['-'] =$pdf->AddLink();
  1285.         $pdf->SetLink($pdf->PMA_links['doc'][$table]['-'],-1);
  1286.         $pdf->SetFont('', 'B',18);
  1287.         $pdf->Cell(0,8, $z .' '.$table,1,1,'C',0,$pdf->PMA_links['RT'][$table]['-']);
  1288.         $pdf->SetFont('', '',8);
  1289.         $pdf->ln();
  1290.  
  1291.         $cfgRelation  = PMA_getRelationsParam();
  1292.         if ($cfgRelation['commwork']) {
  1293.             $comments = PMA_getComments($db, $table);
  1294.         }
  1295.         if ($cfgRelation['mimework']) {
  1296.             $mime_map = PMA_getMIME($db, $table, true);
  1297.         }
  1298.  
  1299.         /**
  1300.          * Gets table informations
  1301.          */
  1302.         // The 'show table' statement works correct since 3.23.03
  1303.         if (PMA_MYSQL_INT_VERSION >= 32303) {
  1304.              $local_query  = "SHOW TABLE STATUS LIKE '" . PMA_sqlAddslashes($table, TRUE) . "'";
  1305.              $result       = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
  1306.              $showtable    = PMA_mysql_fetch_array($result);
  1307.              $num_rows     = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
  1308.              $show_comment = (isset($showtable['Comment']) ? $showtable['Comment'] : '');
  1309.              $create_time  = (isset($showtable['Create_time']) ? PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
  1310.              $update_time  = (isset($showtable['Update_time']) ? PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
  1311.              $check_time   = (isset($showtable['Check_time']) ? PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
  1312.         } else {
  1313.              $showtable    = array();
  1314.              $num_rows     = PMA_countRecords($db, $table, TRUE);
  1315.              $show_comment = '';
  1316.              $create_time  = '';
  1317.              $update_time  = '';
  1318.              $check_time   = '';
  1319.         } // end display comments
  1320.         if ($result) {
  1321.              mysql_free_result($result);
  1322.         }
  1323.  
  1324.  
  1325.         /**
  1326.          * Gets table keys and retains them
  1327.          */
  1328.         $local_query  = 'SHOW KEYS FROM ' . PMA_backquote($table);
  1329.         $result       = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
  1330.         $primary      = '';
  1331.         $indexes      = array();
  1332.         $lastIndex    = '';
  1333.         $indexes_info = array();
  1334.         $indexes_data = array();
  1335.         $pk_array     = array(); // will be use to emphasis prim. keys in the table
  1336.                                  // view
  1337.         while ($row = PMA_mysql_fetch_array($result)) {
  1338.             // Backups the list of primary keys
  1339.             if ($row['Key_name'] == 'PRIMARY') {
  1340.                 $primary   .= $row['Column_name'] . ', ';
  1341.                 $pk_array[$row['Column_name']] = 1;
  1342.             }
  1343.             // Retains keys informations
  1344.             if ($row['Key_name'] != $lastIndex ){
  1345.                 $indexes[] = $row['Key_name'];
  1346.                 $lastIndex = $row['Key_name'];
  1347.             }
  1348.             $indexes_info[$row['Key_name']]['Sequences'][]     = $row['Seq_in_index'];
  1349.             $indexes_info[$row['Key_name']]['Non_unique']      = $row['Non_unique'];
  1350.             if (isset($row['Cardinality'])) {
  1351.                 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
  1352.             }
  1353.             // I don't know what does following column mean....
  1354.             // $indexes_info[$row['Key_name']]['Packed']          = $row['Packed'];
  1355.             $indexes_info[$row['Key_name']]['Comment']         = $row['Comment'];
  1356.  
  1357.             $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name']  = $row['Column_name'];
  1358.             if (isset($row['Sub_part'])) {
  1359.                 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
  1360.             }
  1361.  
  1362.         } // end while
  1363.         if ($result) {
  1364.             mysql_free_result($result);
  1365.         }
  1366.  
  1367.  
  1368.         /**
  1369.          * Gets fields properties
  1370.          */
  1371.         $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table);
  1372.         $result      = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
  1373.         $fields_cnt  = mysql_num_rows($result);
  1374.  
  1375.  
  1376.         // Check if we can use Relations (Mike Beck)
  1377.         if (!empty($cfgRelation['relation'])) {
  1378.             // Find which tables are related with the current one and write it in
  1379.             // an array
  1380.             $res_rel = PMA_getForeigners($db, $table);
  1381.  
  1382.             if (count($res_rel) > 0) {
  1383.                 $have_rel = TRUE;
  1384.             } else {
  1385.                 $have_rel = FALSE;
  1386.             }
  1387.         }
  1388.         else {
  1389.             $have_rel = FALSE;
  1390.         } // end if
  1391.  
  1392.  
  1393.         /**
  1394.          * Displays the comments of the table if MySQL >= 3.23
  1395.          */
  1396.  
  1397.         $break = false;
  1398.         if (!empty($show_comment)) {
  1399.             $pdf->Cell(0,3,$GLOBALS['strTableComments'] . ' : ' . $show_comment,0,1);
  1400.             $break = true;
  1401.         }
  1402.  
  1403.         if (!empty($create_time)) {
  1404.             $pdf->Cell(0,3,$GLOBALS['strStatCreateTime'] . ': ' . $create_time,0,1);
  1405.             $break = true;
  1406.         }
  1407.  
  1408.         if (!empty($update_time)) {
  1409.             $pdf->Cell(0,3,$GLOBALS['strStatUpdateTime'] . ': ' . $update_time,0,1);
  1410.             $break = true;
  1411.         }
  1412.  
  1413.         if (!empty($check_time)) {
  1414.             $pdf->Cell(0,3,$GLOBALS['strStatCheckTime'] . ': ' . $check_time,0,1);
  1415.             $break = true;
  1416.         }
  1417.  
  1418.         if ($break == true) {
  1419.             $pdf->Cell(0,3,'',0,1);
  1420.             $pdf->Ln();
  1421.         }
  1422.  
  1423.         $i = 0;
  1424.         $pdf->SetFont('', 'B');
  1425.         if (isset($orientation) && $orientation == 'L') {
  1426.             $pdf->Cell(25,8,ucfirst($GLOBALS['strField']),1,0,'C');
  1427.             $pdf->Cell(20,8,ucfirst($GLOBALS['strType']),1,0,'C');
  1428.             $pdf->Cell(20,8,ucfirst($GLOBALS['strAttr']),1,0,'C');
  1429.             $pdf->Cell(10,8,ucfirst($GLOBALS['strNull']),1,0,'C');
  1430.             $pdf->Cell(20,8,ucfirst($GLOBALS['strDefault']),1,0,'C');
  1431.             $pdf->Cell(25,8,ucfirst($GLOBALS['strExtra']),1,0,'C');
  1432.             $pdf->Cell(45,8,ucfirst($GLOBALS['strLinksTo']),1,0,'C');
  1433.             $pdf->Cell(67,8,ucfirst($GLOBALS['strComments']),1,0,'C');
  1434.             $pdf->Cell(45,8,'MIME',1,1,'C');
  1435.             $pdf->SetWidths(array(25,20,20,10,20,25,45,67,45));
  1436.         } else {
  1437.             $pdf->Cell(20,8,ucfirst($GLOBALS['strField']),1,0,'C');
  1438.             $pdf->Cell(20,8,ucfirst($GLOBALS['strType']),1,0,'C');
  1439.             $pdf->Cell(20,8,ucfirst($GLOBALS['strAttr']),1,0,'C');
  1440.             $pdf->Cell(10,8,ucfirst($GLOBALS['strNull']),1,0,'C');
  1441.             $pdf->Cell(15,8,ucfirst($GLOBALS['strDefault']),1,0,'C');
  1442.             $pdf->Cell(15,8,ucfirst($GLOBALS['strExtra']),1,0,'C');
  1443.             $pdf->Cell(30,8,ucfirst($GLOBALS['strLinksTo']),1,0,'C');
  1444.             $pdf->Cell(30,8,ucfirst($GLOBALS['strComments']),1,0,'C');
  1445.             $pdf->Cell(30,8,'MIME',1,1,'C');
  1446.             $pdf->SetWidths(array(20,20,20,10,15,15,30,30,30));
  1447.         }
  1448.         $pdf->SetFont('', '');
  1449.  
  1450.         while ($row = PMA_mysql_fetch_array($result)) {
  1451.             $bgcolor = ($i % 2) ?$GLOBALS['cfg']['BgcolorOne'] : $GLOBALS['cfg']['BgcolorTwo'];
  1452.             $i++;
  1453.  
  1454.             $type             = $row['Type'];
  1455.             // reformat mysql query output - staybyte - 9. June 2001
  1456.             // loic1: set or enum types: slashes single quotes inside options
  1457.             if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)) {
  1458.                 $tmp[2]       = substr(ereg_replace("([^,])''", "\\1\\'", ',' . $tmp[2]), 1);
  1459.                 $type         = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
  1460.                 $type_nowrap  = '';
  1461.  
  1462.                 $binary       = 0;
  1463.                 $unsigned     = 0;
  1464.                 $zerofill     = 0;
  1465.             } else {
  1466.                 $type_nowrap  = ' nowrap="nowrap"';
  1467.                 $type         = eregi_replace('BINARY', '', $type);
  1468.                 $type         = eregi_replace('ZEROFILL', '', $type);
  1469.                 $type         = eregi_replace('UNSIGNED', '', $type);
  1470.                 if (empty($type)) {
  1471.                     $type     = ' ';
  1472.                 }
  1473.  
  1474.                 $binary       = eregi('BINARY', $row['Type'], $test);
  1475.                 $unsigned     = eregi('UNSIGNED', $row['Type'], $test);
  1476.                 $zerofill     = eregi('ZEROFILL', $row['Type'], $test);
  1477.             }
  1478.             $strAttribute     = ' ';
  1479.             if ($binary) {
  1480.                 $strAttribute = 'BINARY';
  1481.             }
  1482.             if ($unsigned) {
  1483.                 $strAttribute = 'UNSIGNED';
  1484.             }
  1485.             if ($zerofill) {
  1486.                 $strAttribute = 'UNSIGNED ZEROFILL';
  1487.             }
  1488.             if (!isset($row['Default'])) {
  1489.                 if ($row['Null'] != '') {
  1490.                     $row['Default'] = 'NULL';
  1491.                 }
  1492.             }
  1493.             $field_name = $row['Field'];
  1494.             //$pdf->Ln();
  1495.             $pdf->PMA_links['RT'][$table][$field_name] =$pdf->AddLink();
  1496.             $pdf->Bookmark($field_name,1,-1);
  1497.             $pdf->SetLink($pdf->PMA_links['doc'][$table][$field_name],-1);
  1498.             $pdf_row = array($field_name ,
  1499.                             $type ,
  1500.                             $strAttribute ,
  1501.                             ($row['Null'] == '') ? $GLOBALS['strNo'] : $GLOBALS['strYes'],
  1502.                             ((isset($row['Default'])) ?  $row['Default'] : ''),
  1503.                             $row['Extra']  ,
  1504.                             ((isset($res_rel[$field_name])) ? $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
  1505.                             ((isset($comments[$field_name])) ? $comments[$field_name]  : '' ),
  1506.                             ((isset($mime_map) && isset($mime_map[$field_name])) ? str_replace('_', '/', $mime_map[$field_name]['mimetype'])  : '' )
  1507.                             );
  1508.             $links[0] = $pdf->PMA_links['RT'][$table][$field_name];
  1509.             if (isset($res_rel[$field_name]['foreign_table']) AND
  1510.                 isset($res_rel[$field_name]['foreign_field']) AND
  1511.                 isset($pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
  1512.               ) $links[6] = $pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
  1513.               else unset($links[6]);
  1514.             $pdf->Row($pdf_row, $links);
  1515.  
  1516.              /*$pdf->Cell(20,8,$field_name,1,0,'L',0,$pdf->PMA_links['RT'][$table][$field_name]);
  1517.                 //echo '    ' . $field_name . ' ' . "\n";
  1518.             }
  1519.         $pdf->Cell(20,8,$type,1,0,'L');
  1520.         $pdf->Cell(20,8,$strAttribute,1,0,'L');
  1521.         $pdf->Cell(15,8,,1,0,'L');
  1522.         $pdf->Cell(15,8,((isset($row['Default'])) ?  $row['Default'] : ''),1,0,'L');
  1523.         $pdf->Cell(15,8,$row['Extra'],1,0,'L');
  1524.            if ($have_rel) {
  1525.                 if (isset($res_rel[$field_name])) {
  1526.                     $pdf->Cell(30,8,$res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'],1,0,'L');
  1527.                 }
  1528.             }
  1529.             if ($cfgRelation['commwork']) {
  1530.                 if (isset($comments[$field_name])) {
  1531.                     $pdf->Cell(0,8,$comments[$field_name],1,0,'L');
  1532.                 }
  1533.             } */
  1534.         } // end while
  1535.         $pdf->SetFont('', '',14);
  1536.         mysql_free_result($result);
  1537.     }//end each
  1538.  
  1539.  
  1540. } // end function PMA_RT_DOC
  1541.  
  1542.  
  1543. /**
  1544.  * Main logic
  1545.  */
  1546. if (!isset($pdf_page_number)) {
  1547.     $pdf_page_number  = 1;
  1548. }
  1549. $show_grid            = (isset($show_grid) && $show_grid == 'on') ? 1 : 0;
  1550. $show_color           = (isset($show_color) && $show_color == 'on') ? 1 : 0;
  1551. $show_table_dimension = (isset($show_table_dimension) && $show_table_dimension == 'on') ? 1 : 0;
  1552. $all_tab_same_wide    = (isset($all_tab_same_wide) && $all_tab_same_wide == 'on') ? 1 : 0;
  1553. $with_doc             = (isset($with_doc) && $with_doc == 'on') ? 1 : 0;
  1554. $orientation          = (isset($orientation) && $orientation == 'P') ? 'P' : 'L';
  1555. $paper                = isset($paper) ? $paper : 'A4';
  1556. PMA_mysql_select_db($db);
  1557.  
  1558. $rt = new PMA_RT($pdf_page_number, $show_table_dimension, $show_color, $show_grid, $all_tab_same_wide, $orientation, $paper);
  1559. ?>
  1560.